library(magrittr)
library(stringr)
library(plyr)
library(tidyverse)
library(stargazer)


rm(list=ls())
home = 'C:/Users/Jason/Dropbox/VNA_Responsiveness/Analysis/JOP-dataverse/'


permutation.results = paste0(home, 'RI-analyses.Rds') %>%
  readRDS
experimental.results = paste0(home, 'experimental-analyses.Rds') %>%
  readRDS


columns = c('tab4col1','tab4col2','tab4col3','tab4col4')
significance = subset(permutation.results, 
                      Result %in% columns) %>%
  dlply(.variables='Result', 
        .fun=function(column) {
          ddply(.data=column,
                .variables='term',
                .fun=function(x) {
                  est = subset(experimental.results, 
                               Result==x$Result[1] & term==x$term[1])$estimate
                  ris = x$estimate
                  qest = ecdf(ris)
                  Q = qest(est) %>%
                    round(digits=3) %>%
                    str_pad(width=5, side='right', pad=0) %>%
                    paste0('(', ., ')')
                  est = est %>%
                    round(digits=3) %>%
                    str_pad(width=5, side='right', pad=0)
                  data.frame(output=paste0(est, '\n', Q),
                             stringsAsFactors=F) %>%
                    set_colnames(x$Result[1])
                }, .inform=T)
        }, .progress='text', .inform=T)


output = merge(significance[['tab4col1']], significance[['tab4col2']], by='term', all=T) %>%
  merge(significance[['tab4col3']], by='term', all=T) %>%
  merge(significance[['tab4col4']], by='term', all=T) %>%
  set_rownames(.[,'term']) %>%
  subset(select=-term)
output = output[c('Citizen',
                  'Firm',
                  'FullTime',
                  'CentNom',
                  'Competitive',
                  '(Intercept)'),] %>%
  adply(.margins=1, .fun=function(x) {
    x1 = str_extract(string=unlist(x), pattern='.*(?=\\n)')
    x2 = str_extract(string=unlist(x), pattern='(?<=\\n).*')
    rbind(x1, x2) %>%
      set_colnames(colnames(x))
  }) %>%
  sapply(FUN=as.character) %>%
  as.data.frame(stringsAsFactors=F)


c(subset(experimental.results, Result=='tab4col1')$N[1],
  subset(experimental.results, Result=='tab4col2')$N[1],
  subset(experimental.results, Result=='tab4col3')$N[1],
  subset(experimental.results, Result=='tab4col4')$N[1]) %>%
  matrix(nrow=1) %>%
  as.data.frame %>%
  set_colnames(colnames(output)) %>%
  rbind(output, .) %>%
  set_rownames(c('% Citizen','2',
                 '% Firm','5',
                 '% Full-time','7',
                 '% Central nominee','8',
                 '% Competitive','9',
                 'Constant','10',
                 'Observations')) %>%
  stargazer(summary=F, 
            header=F,
            align=T,
            notes.append=F,
            notes='Randomization inference based on 10,000 randomizations. Quantile of experimental estimate in parentheses. The unit of analysis is the province.',
            title='Delegate mentioned province.')

